﻿using System.IO;
using System.Text;
using System.Windows.Forms;

namespace CDude.Tools
{
    internal class TextBoxWriter : TextWriter
    {
        private char previousvalue = '\b';
        private TextBox textbox;

        public TextBoxWriter(TextBox textbox)
        {
            this.textbox = textbox;
        }

        public override Encoding Encoding
        {
            get { return Encoding.UTF8; }
        }

        public override void Write(char value)
        {
            if (value == '\n' && previousvalue == '\n') return;
            previousvalue = value;
            base.Write(value);
            textbox.Text += value;
            textbox.SelectionStart = textbox.Text.Length;
            textbox.ScrollToCaret();
        }
    }
}